<?php

abstract class Gallery_Utils 
{
    
    /**
    * Photo storage
    * 
    * @return string
    */
    static public function photoSorage() 
    {
        return SITE_ROOT . '/' . Kernel_Config::get('gallery', 'photo_sorage');
    }
    
    /**
    * Photo thumb storage
    * 
    * @return string
    */
    static public function photoThumbSorage() 
    {
        return SITE_ROOT . '/' . Kernel_Config::get('gallery', 'photo_thumbs_sorage');
    }
    
    /**
    * Return photo filename for the record with id specified.
    * 
    * @param integer $id
    * @return string
    */
    static public function getFileName($id) 
    {
        static $cach = array();
        $id = (int)$id;
        
        if (!array_key_exists($id, $cach)) {
            $query = '
                SELECT 
                       `file` 
                  FROM 
                       `photos` 
                 WHERE 
                       `id` = ' . $id;
            $cach[$id] = database::fetchOne($query);
        }
        
        return $cach[$id];
    }
    
    /**
    * Unlink file with specified name.
    * 
    * @param string $file_name
    */
    static public function unlinkFile($file_name) 
    {
        if (file_exists(self::photoSorage() . $file_name)) {
            unlink(self::photoSorage() . $file_name);
        }
        if (file_exists(self::photoThumbSorage() . $file_name)) {
            unlink(self::photoThumbSorage() . $file_name);
        }
    }
    
}